package com.mbyte.easy.stu.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mbyte.easy.common.controller.BaseController;
import com.mbyte.easy.common.web.AjaxResult;
import com.mbyte.easy.stu.entity.TStudents;
import com.mbyte.easy.stu.service.ITStudentsService;
import com.mbyte.easy.util.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.List;

/**
 * lplplp
 */

@Controller
@RequestMapping("/stu/tStudents")
public class TStudentsController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(com.mbyte.easy.stu.controller.TStudentsController.class);

    private String prefix = "stu/tStudents/";

    @Autowired
    private ITStudentsService itStudentsService;


    /**
     * @return
     */

    @RequestMapping
    public String index(Model model,
                        @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                        @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize,
                        @RequestParam(value = "stuname", required = false, defaultValue = "") String stuname,
                        @RequestParam(value = "stusex", required = false, defaultValue = "") Byte stusex,
                        @RequestParam(value = "age", required = false, defaultValue = "") Integer age,
                        @RequestParam(value = "stunum", required = false, defaultValue = "") Long stunum,

                        @RequestParam(value = "createtimeSpace", required = false, defaultValue = "") String createtimeSpace,
                        @RequestParam(value = "updatetimeSpace", required = false, defaultValue = "") String updatetimeSpace) {

        TStudents tStudents = new TStudents();

        String startUpdatetime = "";
        String endUpdatetime = "";

        String startCreatetime = "";
        String endCreatetime = "";

        if(updatetimeSpace != null && !"".equals(updatetimeSpace.trim())){

            String[] split = updatetimeSpace.split(" - ");
            startUpdatetime = split[0];
            endUpdatetime = split[1];
        }
        if(createtimeSpace != null && !"".equals(createtimeSpace.trim())){

            String[] split = createtimeSpace.split(" - ");
            startCreatetime = split[0];
            endCreatetime = split[1];
        }

        if (stuname != null && !"".equals(stuname.trim())) {
            tStudents.setStuname(stuname.trim());   //搜索时候设置值

        }



        if(stusex != null){
            if(stusex == 0)
            {
                tStudents.setStusex(0);
            }

            if(stusex == 1)
            {
                tStudents.setStusex(1);
            }
        }

        if (age != null) {
            tStudents.setAge(age);

        }
        if (stunum != null) {
            tStudents.setStunum(stunum);

        }



        Page<TStudents> page = new Page<TStudents>(pageNo, pageSize);

        IPage<TStudents> pageInfo = itStudentsService.find(page,tStudents,startUpdatetime,endUpdatetime,startCreatetime,endCreatetime);
        model.addAttribute("createtimeSpace",createtimeSpace);
        model.addAttribute("updatetimeSpace",updatetimeSpace);
        model.addAttribute("searchInfo",tStudents);
        model.addAttribute("pageInfo", new PageInfo<TStudents>(pageInfo));
        return this.prefix + "list";
    }

    /**
     * 添加跳转页面
     * @return
     */
    @RequestMapping("addBefore")

    public String addBefore(){
        return prefix+"add";
    }

    /**
     * 添加
     * @param tStudents
     * @return
     */
    @PostMapping("add")
    @ResponseBody
    public AjaxResult add(TStudents tStudents){


        LocalDateTime now =  LocalDateTime.now();
        tStudents.setCreatetime(now);
        tStudents.setUpdatetime(now);
        return toAjax(itStudentsService.insert(tStudents));
    }


    /**
     * 添加跳转页面
     * @return
     */
    @RequestMapping("editBefore/{id}")
    public String editBefore(Model model,@PathVariable("id")Long id){
        model.addAttribute("tStudents",itStudentsService.selectByPrimaryKey(id));
        return prefix+"edit";
    }
    /**
     * 添加
     * @param tStudents
     * @return
     */
    @PostMapping("edit")
    @ResponseBody
    public AjaxResult edit(TStudents tStudents){
        LocalDateTime now =  LocalDateTime.now();
        tStudents.setCreatetime(now);
        tStudents.setUpdatetime(now);

        return toAjax(itStudentsService.updateByPrimaryKey(tStudents));
    }
    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("delete/{id}")
    @ResponseBody
    public AjaxResult delete(@PathVariable("id") Long id)
    {
        return toAjax(itStudentsService.deleteByPrimaryKey(id));
    }
    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PostMapping("deleteAll")
    @ResponseBody
    public AjaxResult deleteAll(@RequestBody List<Long> ids){
        return toAjax(itStudentsService.deleteAll(ids));
    }
}
